/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entity.IndicativeProductPrice;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Samuel
 */
@Stateless
@LocalBean
public class IndicativeProductPriceManager {

    @PersistenceContext
    private EntityManager em;
    private IndicativeProductPrice productPrice;
    private Collection<IndicativeProductPrice> productsList = new ArrayList<IndicativeProductPrice>();

    public void createIndicativeProductPrice(String productName, String businessType, Double price) {
        productPrice = new IndicativeProductPrice();
        productPrice.setProductName(productName);
        productPrice.setBusinessType(businessType);
        productPrice.setPrice(price);
        em.persist(productPrice);
        em.flush();
    }

    public void removeIndicativeProductPrice(Long id) {
        IndicativeProductPrice i;
        i = em.find(IndicativeProductPrice.class, id);
        em.remove(i);
        em.flush();
    }

    public void editIndicativeProductPrice(Long id, String productName, String businessType, Double price) {
        IndicativeProductPrice i;
        i = em.find(IndicativeProductPrice.class, id);
        i.setProductName(productName);
        i.setBusinessType(businessType);
        i.setPrice(price);
        em.flush();
    }

    /**
     * @return the productsList
     */
    public Collection<IndicativeProductPrice> getProductsList(String businessType) {

        productsList = new ArrayList<IndicativeProductPrice>();
        Query q = em.createQuery("Select i From IndicativeProductPrice i where i.businessType = :businessType");
        q.setParameter("businessType", businessType);
        for (Object o : q.getResultList()) {
            IndicativeProductPrice temp = (IndicativeProductPrice) o;
            productsList.add(temp);
        }
        return productsList;
    }

    public Collection<IndicativeProductPrice> getProductsList() {

        productsList = new ArrayList<IndicativeProductPrice>();
        Query q = em.createQuery("Select i From IndicativeProductPrice i");
        for (Object o : q.getResultList()) {
            IndicativeProductPrice temp = (IndicativeProductPrice) o;
            productsList.add(temp);
        }
        return productsList;
    }

    /**
     * @return the em
     */
    public EntityManager getEm() {
        return em;
    }

    /**
     * @param em the em to set
     */
    public void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * @return the productPrice
     */
    public IndicativeProductPrice getProductPrice() {
        return productPrice;
    }

    /**
     * @param productPrice the productPrice to set
     */
    public void setProductPrice(IndicativeProductPrice productPrice) {
        this.productPrice = productPrice;
    }

    /**
     * @param productsList the productsList to set
     */
    public void setProductsList(Collection<IndicativeProductPrice> productsList) {
        this.productsList = productsList;
    }
}
